define(function(require, exports, module) {

	"use strict";
	require("jquery");
	require("mui");
	require("../newOrder/dateInput.js");

	var getFunList = require("../getFunList/getFunList.js"); //函数配置js

	getFunList.setParm(); //税率与折扣 权限控制
	getFunList.infoFromOrderShow(); //编辑新增表单页面点击货品信息进入编辑

	var updateSell = require("../newOrder/updateSell");
	//调拨记录标识
	var productSelectSource = sessionStorage.getItem("productSelectSource");
	//后台数据修改
	var statusType = sessionStorage.getItem("statusType");
	var sellId = sessionStorage.getItem("sellId"); //销售记录ID
	var PirchaseID = sessionStorage.getItem("PirchaseID"); //采购记录ID
	var allocateID = sessionStorage.getItem("allocateID"); //调拨记录ID
	var outPirchaseID = sessionStorage.getItem("outPirchaseID"); //采购入库ID
	var salesOutId = sessionStorage.getItem("salesOutId"); //销售出库ID
	//→销售订单
	if(sellId && statusType) {
		updateSell.sellDeta();
	}
	//→采购订单
	if(PirchaseID && statusType) {
		updateSell.buyDeta();

	}
	//→库存调拨
	if(allocateID && statusType) {
		updateSell.allotStockDeta();
	}
	//→采购入库订单
	if(outPirchaseID && statusType) {
		updateSell.buyInputDeta();
	}
	//→销售出库记录订单
	if(salesOutId && statusType) {
		updateSell.outSalesDeta();
	}
	//本地数据修改
	var localstatusType = sessionStorage.getItem("localstatusType");
	if(localstatusType) {
		updateSell.localDeta();
	}
	$(function() {

		if(sessionStorage.money) {
			$(".fromInstock").show();
		} else {
			$(".fromInstock").hide();
		}
		if(sessionStorage.goodsInfoList) { //定义货品信息列表本地变量
			var goodsInfoList = eval(sessionStorage.goodsInfoList);
		} else {
			var goodsInfoList = [];
		}
		//点击完成时触发
		mui("body").on("tap", "#finish-btn", function() {
			//				var statusType = sessionStorage.getItem("statusType")
			//			var localstatusType = sessionStorage.getItem("localstatusType");
			//有这个状态PUSH 销售
			if(!statusType && !localstatusType) {
				subProductDetail();
			}
			//点击完成保存更新后的后台数据→销售订单
			if(sellId && statusType) {
				updateSell.saveSellDeta();
			}
			//点击完成保存更新后的后台数据→采购订单
			if(PirchaseID && statusType) {
				updateSell.saveBuyDeta();
			}
			//点击完成保存更新后的后台数据→库存调拨
			if(allocateID && statusType) {
				updateSell.saveAllotStockDeta();
			}
			//点击完成保存更新后的后台数据→采购入库订单
			if(outPirchaseID && statusType) {
				updateSell.saveBuyInputDeta();
			}
			//点击完成保存更新后的后台数据→销售出库库订单
			if(salesOutId && statusType) {
				updateSell.saveOutSalesDeta();
			}
			//点击完成保存更新后的本地新增数据
			if(localstatusType) {
				updateSell.savelocalDeta();
			}

		});

		function alertTshow(i) {
			$(".alertT").eq(i).stop().animate({
				"top": "70px"
			}, 400, function() {
				$(this).delay(1000).fadeOut(500, function() {
					$(this).css("top", "-35px");
					$(this).show();
				});
			});
		}

		function ADDinvalid(i) {
			$(".testI").eq(i).addClass("invalid");
			$(".testI").eq(i).attr("placeholder", "不能为空");
		}

		function REMOVEinvalid(i) {
			$(".testI").eq(i).removeClass("invalid");
		}

		function subProductDetail() { //提交数据函数
			var goodsBuyNum = $("#goodsNum").val(); //购买数量
			var goodsPrice = $("#goodsPrice").val(); //产品单价
			var goodsWhid = $("#whid").data("id"); //仓库ID
			var goodsWhName = $("#whid").text(); //仓库名字
			if(!goodsWhid) {
				$("#whid").addClass("red");
			} else {
				$("#whid").removeClass("red");
			}

			if(!goodsBuyNum) {
				ADDinvalid(0);
			} else {
				REMOVEinvalid(0);
			}

			if(!productSelectSource) {

				if(!goodsPrice) {
					ADDinvalid(1);
				} else {
					REMOVEinvalid(0);
				}
				if(!goodsPrice) {
					alertTshow(2);
					return;
				}
			}

			if(!goodsBuyNum) {
				alertTshow(1);
				return;
			}
			if(!goodsWhid) {
				alertTshow(0);
				return;
			}

			var goodsSerial = $("#goodsSerial").text();
			var goodsName = $("#goodsName").text();
			var goodsId = $("#goodsName").data("id");
			var goodsUnit = $("#goodsName").data("unit");
			var goodsImg = $(".product-img").attr("src");
			var goodsBuyNum = $("#goodsNum").val(); //购买数量
			var goodsPrice = $("#goodsPrice").val(); //产品单价
			if(!goodsPrice) {
				goodsPrice = 0;
			}
			var allMoney = goodsBuyNum * goodsPrice; //产品数量*单价（无折扣）
			var taxes = Number($("#taxes").val()) / 100; //税率
			var taxesMoney = $("#taxesMoney").text(); //税金
			var noTaxesMoney = $("#noTaxesMoney").text(); //未税金额
			var hasTaxesMoney = $("#hasTaxesMoney").text(); //含税金额
			var discount = Number($("#discount").val()) / 100; //折扣率
			if(!discount || discount == 0) {
					discount = 100;
				}
			var discountMoney = $("#discountMoney").val(); //折扣额
			var lastMoney = $("#lastMoney").text(); //折后金额
			var notes = $("#notes").val(); //备注
			var goodsStock = $("#goodsStock").text();

			var goodsInfo = {
				"goodsSerial": goodsSerial,
				"goodsId": goodsId,
				"goodsName": goodsName,
				"goodsImg": goodsImg,
				"goodsBuyNum": goodsBuyNum,
				"goodsPrice": goodsPrice,
				"goodsStock": goodsStock,
				"goodsWhid": goodsWhid,
				"goodsWhName": goodsWhName,
				"goodsUnit": goodsUnit,
				"lastMoney": lastMoney,
				"allMoney": Number(goodsBuyNum) * Number(goodsPrice),
				"taxes": taxes,
				"taxesMoney": taxesMoney,
				"noTaxesMoney": noTaxesMoney,
				"hasTaxesMoney": hasTaxesMoney,
				"discount": discount,
				"discountMoney": discountMoney,
				"lastMoney": lastMoney,
				"notes": notes
			};
			sessionStorage.removeItem("goodsSerial");
			sessionStorage.removeItem("goodsName");
			sessionStorage.removeItem("goodsStock");
			sessionStorage.removeItem("goodsId");
			sessionStorage.removeItem("libraryName");
			sessionStorage.removeItem("libraryNameId");
			sessionStorage.removeItem("data-stockQuery");
			//			var statusType = sessionStorage.getItem("statusType")
			//有这个状态PUSH 销售
			//			if(!statusType && statusType == undefined){
			goodsInfoList.push(JSON.stringify(goodsInfo));
			//			}
			sessionStorage.goodsInfoList = JSON.stringify(goodsInfoList); //产品信息本地储存
			//			console.log(sessionStorage.goodsInfoList);
			sessionStorage.removeItem("libraryName");
			sessionStorage.removeItem("libraryNameId");
			mui.back();
		}

		function count(i) {
			var goodsBuyNum = $("#goodsNum").val(); //购买数量
			var goodsPrice = $("#goodsPrice").val(); //产品单价
			var discount = $("#discount").val(); //折扣率
			var taxes = $("#taxes").val(); //税率

			$("#fromInstockMoney").text((goodsBuyNum * goodsPrice).toFixed(2));

			if(discount) {
				$("#discount").css("margin-right", "26px");
				$(".pre").eq(0).show();
			}
			if(taxes) {
				$("#taxes").css("margin-right", "26px");
				$(".pre").eq(1).show();
			}

			var normalMoney = Number(goodsBuyNum * goodsPrice).toFixed(2); //正常金额  数量*单价
			var discountMoney = 0; //折扣额
			var lastMoney = 0; //折后金额

			if(i == 1) { //不计税

				if(!discount || discount == 0) {
					discount = 100;
				}
				discountMoney = Number(normalMoney * (1 - discount / 100)).toFixed(2); //折扣额	
				lastMoney = Number(normalMoney - discountMoney).toFixed(2); //折后金额

				$("#discountMoney").val(discountMoney); //折扣额
				$("#lastMoney").text(lastMoney); //折后金额
				$("#noTaxesMoney").text(lastMoney); //未税金额=折后金额
				$("#hasTaxesMoney").text(lastMoney); //含税金额=折后金额
				$("#taxes").val(0); //税率为0
				$("#count").text("0.00"); //税金为0

			}

			if(i == 2) { //不含税价

				if(!discount || discount == 0) {
					discount = 100;
				}
				discountMoney = Number(normalMoney * (1 - discount / 100)).toFixed(2); //折扣额	
				lastMoney = Number(normalMoney - discountMoney).toFixed(2); //折后金额

				$("#discountMoney").val(discountMoney); //折扣额
				$("#lastMoney").text(lastMoney); //折后金额

				$("#noTaxesMoney").text(lastMoney); //不含税价=金额

				var hasTaxesMoney = (lastMoney * (1 + taxes / 100)).toFixed(2); //含税价=不含税价*（1+税率）
				var taxesMoney = (hasTaxesMoney - lastMoney).toFixed(2); //税金=含税价-不含税价	
				$("#hasTaxesMoney").text(hasTaxesMoney);
				$("#taxesMoney").text(taxesMoney);

			}

			if(i == 3) { //含税价

				if(!discount || discount == 0) {
					discount = 100;
				}
				discountMoney = Number(normalMoney * (1 - discount / 100)).toFixed(2); //折扣额	
				lastMoney = Number(normalMoney - discountMoney).toFixed(2); //折后金额

				$("#discountMoney").val(discountMoney); //折扣额
				$("#lastMoney").text(lastMoney); //折后金额

				$("#hasTaxesMoney").text(lastMoney); //含税价=金额

				var noTaxesMoney = (lastMoney / (1 + taxes / 100)).toFixed(2); //不含税价=金额/（1+税率）	
				var taxesMoney = (lastMoney - noTaxesMoney).toFixed(2); //税金=含税价-不含税价
				$("#noTaxesMoney").text(noTaxesMoney);
				$("#taxesMoney").text(taxesMoney);
			}
		}

		$("body").on("keyup", "#discountMoney", function() {
			var discountMoney = $("#discountMoney").val(); //折扣额
			var lastMoney = Number($("#lastMoney").text()); //折后金额
			var discount = Math.round(((lastMoney - discountMoney) / lastMoney) * 100);
			$("#discount").val(discount);

		});

		$("body").on("keyup", "#goodsNum", function() {
			count(sessionStorage.setParm);
		});
		$("body").on("keyup", "#goodsPrice", function() {
			count(sessionStorage.setParm);
		});
		$("body").on("keyup", "#discount", function() {
			count(sessionStorage.setParm);
		});
		$("body").on("keyup", "#taxes", function() {
			count(sessionStorage.setParm);
		});

		count(sessionStorage.setParm);

		$("body").on("focus", "#discount", function() {
			$(this).css("margin-right", "26px");
			$(".pre").eq(0).show();
		});
		$("body").on("focus", "#taxes", function() {
			$(this).css("margin-right", "26px");
			$(".pre").eq(1).show();
		});
		$("body").on("blur", "#discount", function() {
			var thisVal = $(this).val();
			if(!thisVal) {
				$(this).css("margin-right", "26px");
				$(".pre").eq(0).show();
			}
		});
		$("body").on("blur", "#taxes", function() {
			$(this).css("margin-right", "25px");
			$(".pre").eq(1).show();
		});

		//货品id名字库存量 判断条件
		//		var statusType = sessionStorage.getItem("statusType");
		//		var stock = sessionStorage.getItem("stock")
		if(!statusType || !localstatusType) {

			if(sessionStorage.getItem("data-stockQuery")) {
				var goodsFromListPage = JSON.parse(sessionStorage.getItem("data-stockQuery"));
				console.log(goodsFromListPage);
				$("#goodsSerial").text(goodsFromListPage.goodsSerial);
				$("#goodsName").text(goodsFromListPage.goodsName);
				$("#goodsName").attr("data-id", goodsFromListPage.id);
				$("#goodsName").attr("data-unit", goodsFromListPage.unit);
				$("#goodsStock").text(goodsFromListPage.whNumber);
				$("#whid").attr("data-id", sessionStorage.libraryNameId);
				$("#whid").text(sessionStorage.libraryName);

				var ImgSrc = "../../img/chioceProduct/product-icon.png";
				try {
					if(goodsFromListPage.infoGoodsImg[0].imgPath) {
						ImgSrc = goodsFromListPage.infoGoodsImg[0].imgPath;
					}
				} catch(e) {

				}
				$(".product-img").attr("src", ImgSrc);
			}

		}

		var goodsWhid = $("#whid").data("id"); //仓库ID
		var goodsWhName = $("#whid").text();
		//statusType 销售后台数据标识
		//localstatusType 销售本地数据标识
		//stock 库存出入库后台数据标识
		//选择仓库
		if(!statusType && !localstatusType && !sessionStorage.libraryName && !sessionStorage.libraryNameId) {
			$("#whid").text("选择仓库");
			$("#whid").data("id", "");
		} else if(statusType || localstatusType) {
			$("#whid").text(goodsWhName);
			$("#whid").data("id", goodsWhid);
		}
		if(!productSelectSource && productSelectSource != "allot") {
			if(!statusType || !localstatusType || (sessionStorage.libraryName && sessionStorage.libraryNameId)) { //选择仓库
				$("#whid").text(sessionStorage.libraryName);
				$("#whid").data("id", sessionStorage.libraryNameId);
			}
		} else if(sessionStorage.outLibrary) {
			var outWhdata = sessionStorage.outLibrary.split(',');
			//$("#whid").parent("p").parent("a").addClass("mui-disabled");
			$("#whid").parent("p").parent("a").parent("li").hide();
			$("#whid").text(outWhdata[0]);
			$("#whid").data("id", outWhdata[1]);
		}

		getFunList.getGoodsWhInvneotory(); //获取货品各仓库库存量
		//钉钉配置文件
		require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
		var dingTalkNav = require('dd_config');
		//ios
		dingTalkNav.LeftNavBar();
		//ad
		dingTalkNav.leftAndroid();
		dingTalkNav.RightNavBar(false, "更多", "default"); //钉钉右侧按钮隐藏
	});

});